package com.example.freeengine.filetranslate.fragment.setting;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.freeengine.R;
import com.example.freeengine.databinding.FragmentFiletranslateSetBinding;
import com.example.freeengine.filetranslate.FileTranslateViewModel;
import com.example.freeengine.filetranslate.constants.FileTranslateConstant;


public class FiletranslateSetFragment extends Fragment {

    FragmentFiletranslateSetBinding viewBinding;
    FileTranslateViewModel viewModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        viewBinding = FragmentFiletranslateSetBinding.inflate(inflater);
        return viewBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Toolbar toolbar = requireActivity().findViewById(R.id.tb_filetranslate);
        toolbar.setTitle("设置");
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelProvider.AndroidViewModelFactory(requireActivity().getApplication())).get(FileTranslateViewModel.class);

        setNetStatus();

        viewBinding.filetranslateSetNetStatusUpdate.setOnClickListener(v -> {
            setNetStatus();
        });

        viewBinding.filetranslateSetServiceStatusRestart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.startAllServer();
                setNetStatus();
            }
        });

        viewBinding.filetranslateSetServiceStatusStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.stopAllServer();
                setNetStatus();
            }
        });

    }

    private void setNetStatus() {

        int status = viewModel.getNetStatus();

        viewBinding.filetranslateSetNetStatusWifi.setEnabled(false);
        viewBinding.filetranslateSetNetStatusMobile.setEnabled(false);
        viewBinding.filetranslateSetNetStatusNone.setEnabled(false);
        viewBinding.filetranslateSetNetStatusIp.setText("-");

        setBtnUnAvailable();

        // 只有wifi或者也有移动网络
        if (status == 1 || status == 3){
            viewBinding.filetranslateSetNetStatusWifi.setEnabled(true);
            viewBinding.filetranslateSetNetStatusBtnGroup.check(R.id.filetranslate_set_net_status_wifi);

            viewBinding.filetranslateSetNetStatusIp.setText(viewModel.getIp());
            setBtnAvailable();

            if (viewModel.getUploadServerStatus()){
                viewBinding.filetranslateSetServiceReceiveStatusBtn.setText("运行中");
            }else {
                viewBinding.filetranslateSetServiceReceiveStatusBtn.setText("停止");
            }

            if (viewModel.getDownloadServerStatus()){
                viewBinding.filetranslateSetServiceDownloadStatusBtn.setText("运行中");
            }else {
                viewBinding.filetranslateSetServiceDownloadStatusBtn.setText("停止");
            }

            return;
        }else if (status == 2){
            viewBinding.filetranslateSetNetStatusMobile.setEnabled(true);
            viewBinding.filetranslateSetNetStatusBtnGroup.check(R.id.filetranslate_set_net_status_mobile);
        }else {
            viewBinding.filetranslateSetNetStatusNone.setEnabled(true);
            viewBinding.filetranslateSetNetStatusBtnGroup.check(R.id.filetranslate_set_net_status_none);
        }
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(requireContext())
                .setTitle("提示")
                .setMessage("请连接到WiFi网络后重试")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        alertDialog.show();
        viewModel.stopAllServer();

    }

    public void setBtnUnAvailable(){
        viewBinding.filetranslateSetServiceReceiveStatusBtn.setEnabled(false);
        //viewBinding.filetranslateSetServiceReceiveStatusRestartBtn.setEnabled(false);

        viewBinding.filetranslateSetServiceDownloadStatusBtn.setEnabled(false);
        //viewBinding.filetranslateSetServiceUploadStatusRestartBtn.setEnabled(false);

        viewBinding.filetranslateSetServiceReceiveStatusHost.setText("-");
        viewBinding.filetranslateSetServiceUploadStatusHost.setText("-");
    }

    public void setBtnAvailable(){
        viewBinding.filetranslateSetServiceReceiveStatusBtn.setEnabled(true);
        //viewBinding.filetranslateSetServiceReceiveStatusRestartBtn.setEnabled(true);

        viewBinding.filetranslateSetServiceDownloadStatusBtn.setEnabled(true);
        //viewBinding.filetranslateSetServiceUploadStatusRestartBtn.setEnabled(true);

        viewBinding.filetranslateSetServiceReceiveStatusHost.setText("http://" + viewModel.getIp() + ":" +
                FileTranslateConstant.FILETRANSLATE_RECEIVE_HTTP_PORT + FileTranslateConstant.FILETRANSLATE_RECEIVE_HOST_PATH);
        viewBinding.filetranslateSetServiceUploadStatusHost.setText("http://" + viewModel.getIp() + ":" +
                FileTranslateConstant.FILETRANSLATE_DOWNLOAD_HTTP_PORT + FileTranslateConstant.FILETRANSLATE_DOWNLOAD_HOST_PATH);

    }


}